﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class RandomFood : MonoBehaviour
{
    private static RandomFood instance;
    public static RandomFood Instance
    {
        get
        {
            return instance;
        }

    }
    GameObject perfab;
    public Sprite[] render;
    float mapx;
    float mapy;
    Vector3 bound;
    public Vector2 point;
    int xlimit;
    int ylimit;
    private void Awake()
    {
        instance = this;
        Vector2 uisize = GetComponent<RectTransform>().rect.size * 0.8f;
        mapx = uisize.x / 2;
        mapy = uisize.y / 2;
        point.x = transform.position.x - uisize.x;
        point.y = transform.position.y - uisize.y;
    }
    void Start()
    {
        perfab = Resources.Load("jiji/Food") as GameObject;
        
        Creat();
        xlimit = Mathf.RoundToInt(mapx / 45);
        ylimit = Mathf.RoundToInt(mapy / 45);
    }
    void Update()
    {
       

    }
    public void Creat()
    {
        GameObject ob = Instantiate(perfab, transform, false);
        ob.GetComponent<Image>().sprite = render[Random.Range(0, render.Length )];
        ob.transform.localPosition = new Vector3(point.x + Random.Range(0, xlimit) * 30, point.y + Random.Range(0, ylimit) * 30, 0);
        ob.transform.localScale = Vector3.one;
    }
}
